package com.xiaoqian.partyembassy.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.xiaoqian.partyembassy.constant.AppConfig;


/**
 *数据缓存管理类
 */
public class SharedPreUtils {
	private static Context mContext;

	public static void config(Context context) {
		mContext = context;
	}

	/**
	 * 
	 * 描述：将布尔值写入SharedPreferences文件中
	 * 
	 *            上下文
	 * @param result
	 *            boolean
	 * @param filename
	 *            文件名
	 * @param keyword
	 *            boolean的键
	 */
	public static void setBoolean(boolean result, String filename,
			String keyword) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.putBoolean(keyword, result);
		editor.commit();
	}

	/**
	 * 
	 * 描述：获取存储的boolean数据
	 * 
	 * @param filename
	 * @param keyword
	 * @return
	 */
	public static boolean getBoolean(boolean default_result, String filename,
			String keyword) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		boolean result = sp.getBoolean(keyword, default_result);
		return result;
	}

	/**
	 * 
	 * 描述：存储String数据到SharedPreferences中
	 * 
	 * @param result
	 * @param filename
	 * @param keyword
	 */
	public static boolean setString(String result, String filename, String keyword) {
		if (null == mContext) {
			return false;
		}
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.putString(keyword, result);

		return editor.commit();
	}

	/**
	 * 
	 * 描述：获取存储的String数据
	 * 
	 * @param filename
	 *            文件名
	 * @param keyword
	 *            键
	 * @return
	 */
	public static String getString(String filename, String keyword) {
		if (mContext != null) {
			SharedPreferences sp = mContext.getSharedPreferences(filename,
					Context.MODE_MULTI_PROCESS);
			String result = sp.getString(keyword, "");
			return result;
		}
		return "";
	}

	/**
	 * 保存Int
	 * 
	 * @param result
	 * @param filename
	 * @param keyword
	 */
	public static void setInt(int result, String filename, String keyword) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.putInt(keyword, result);
		editor.commit();
	}

	/**
	 * 获取int
	 * 
	 * @param filename
	 * @param keyword
	 * @return
	 */
	public static int getInt(String filename, String keyword) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		return sp.getInt(keyword, 0);
	}
	/**
	 * 保存long
	 *s
	 * @param result
	 * @param filename
	 * @param keyword
	 */
	public static void setLong(long result, String filename, String keyword) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.putLong(keyword, result);
		editor.commit();
	}
	/**
	 * 获取long
	 *
	 * @param filename
	 * @param keyword
	 * @return
	 */
	public static long getLong(String filename, String keyword) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		return sp.getLong(keyword, 0);
	}
	public static void clearAllData(String filename) {
		SharedPreferences sp = mContext.getSharedPreferences(filename,
				Context.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}

	public static String getShareData(String key) {
		SharedPreferences sp = mContext.getSharedPreferences(
				AppConfig.SAVEPATHNAME, Context.MODE_PRIVATE);
		return sp.getString(key, "");
	}

	public static int getIntShareData(String key, int defValue) {
		SharedPreferences sp = mContext.getSharedPreferences(
				AppConfig.SAVEPATHNAME, Context.MODE_PRIVATE);
		return sp.getInt(key, defValue);
	}

	public static void putShareData(String key, String value) {
		SharedPreferences sp = mContext.getSharedPreferences(
				AppConfig.SAVEPATHNAME, Context.MODE_PRIVATE);
		Editor et = sp.edit();
		et.putString(key, value);
		et.commit();
	}

	public static void putIntShareData(String key, int value) {
		SharedPreferences sp = mContext.getSharedPreferences(
				AppConfig.SAVEPATHNAME, Context.MODE_PRIVATE);
		Editor et = sp.edit();
		et.putInt(key, value);
		et.commit();
	}

}
